package com.chinatechstar.component.commons.handler;

import org.apache.ibatis.type.BigDecimalTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Rei
 * @date 2021/7/17
 */
@MappedJdbcTypes({JdbcType.DECIMAL})
@MappedTypes({BigDecimal.class})
public class KeepTowDecimalTypeHandler extends BigDecimalTypeHandler {
    @Override
    public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return super.getNullableResult(rs, columnName) == null ? null : super.getNullableResult(rs, columnName).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return super.getNullableResult(rs, columnIndex) == null ? null : super.getNullableResult(rs, columnIndex).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return super.getNullableResult(cs, columnIndex) == null ? null : super.getNullableResult(cs, columnIndex).setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
